import unit from '@/api/unit'
export default {
    data(){
        return{
            currentNode:null,
            parentNode:null,
            depts: [],
            rootKey:'',
            expandKeys:[],
            defaultProps: {
                children: 'depts',
                label: 'bmmc',
                isLeaf: 'leaf'
            }
        }
    },
    methods:{
        handleNodeClick(data,node,tree){

            this.currentNode = node;
        },
        loadTree(){

            unit.queryDeptTree()
            .then(response => {
                this.loading = false;
                const { data } = response;
                //console.log(data)
                if( data.error_code == 0 ){
                    
                    if( data.list.length > 0 ){
                        this.rootKey = data.list[0].bmbh;
                        this.expandKeys.push(this.rootKey);
                    }
                    this.depts = data.list;
                    //this.$store.commit('app/setAuthInfo',data);
                }else{
                    //this.showError('获取授权信息失败！');
                }
            }).catch(error => {
                this.loading = false;
                //this.showError('网络错误，获取授权信息失败！');
            });
        },
        setFormValue(node){

        },
        initCurrentNode(){
            const tree = this.$refs.deptTree;
            const that = this;
            let count = 0;
            const timer = setInterval(()=>{

                const node = tree.getNode(that.rootKey);
                if( node ){
                        clearInterval(timer);
                        that.currentNode = node;
                        that.parentNode = node.parent;
                        that.setFormValue(node);
                        tree.setCurrentKey(that.rootKey);
                }else{
                    if( count >=10){
                        clearInterval(timer);
                    }
                }
                count ++;
            },300)
        },
        insertNodeData(node,data){

            if( !data ){
                data = node;
                node = this.currentNode;
            }

            const deptTree = this.$refs.deptTree;
            const childNodes = node.childNodes;
            if( childNodes.length == 0 ){
                deptTree.append(data,node);
            }else{
                
                let bFind = false;
                let refNode = null;
                for(let i = childNodes.length -1; i >=0; i -- ){

                    refNode = childNodes[i];
                    if( refNode.data.bmxh <= data.bmxh ){
                        bFind = true;
                        break;
                    }
                }
                if( bFind ){
                    deptTree.insertAfter(data,refNode);
                }else{
                    deptTree.insertBefore(data,refNode);
                }
            }
            this.$nextTick(()=>{
                if( !node.expanded ){
                    node.expand();
                }
            })
        },
        saveNodeData(data){

            const deptTree = this.$refs.deptTree;
            const node = deptTree.getNode(data.bmbh);
            const currentData = node.data;
            const childNodes = node.parent.childNodes;

            if( data.bmxh != currentData.bmxh && childNodes.length > 1){

                console.log(data)
                const parent = node.parent;
                deptTree.remove(node);
                this.insertNodeData(parent,data);
            }else{
                for(let key in currentData ){

                    if( data[key] && currentData[key] !== data[key] ){
    
                        this.$set(currentData,key,data[key])
                    }
                }
            }
        },
        deleteNodeData(node){

            const deptTree = this.$refs.deptTree;
            if(node.data.cdbh === 'root'){
                for(let i = node.childNodes.length; i >=0; i -- ){
                    deptTree.remove(node.childNodes[i]);
                }
                //deptTree.setCurrentKey(node.data.cdbh);
                //this.currentNode = deptTree.getNode(node.data.cdbh);
            }else{
               // const parent = node.parent;
                deptTree.remove(node);
                //deptTree.setCurrentKey(parent.data.cdbh);
                //this.currentNode = deptTree.getNode(parent.data.cdbh);
            }
            this.setFormValue(this.currentNode);
        },
        findNode(bmbh){
            const deptTree = this.$refs.deptTree;
            return deptTree.getNode(bmbh);
        },
        getNodeIcon(node){

            if( node.isLeaf ){
                return <i class="rs-icon-file-fill0"></i>
            }
            return node.expanded?<i class="rs-icon-folder-open-fill"></i>:<i class="rs-icon-folder-fill"></i>
        },
        renderContent(h, { node, data, store }) {

            //console.log(arguments)
            return (
            <span class="rs-tree-node icon">
                {this.getNodeIcon(node)}
                <span class="label">{node.label}</span>
            </span>);
        }
    }
}